package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.dao.Checkgroup_CheckitemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckgroupController {
    @Resource
    private CheckgroupService checkgroupService;
    @Resource private CheckItemDao checkItemDao;
    @Resource private Checkgroup_CheckitemDao checkgroup_checkitemDao;
    @GetMapping("/findPage")
    public Result SelectPage(QueryPageBean queryPageBean){
        PageResult pageResult = checkgroupService.SelectPage(queryPageBean);
        log.warn("{}",pageResult);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);

    }
    @PostMapping("/add")
    public Result Add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        checkgroupService.Add(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ACTION_SUCCESS,null);
    }
    @GetMapping("/findById")
    public Result findById(Integer id) {
        log.info("[检查项-根据ID查询]id:{}", id);
        CheckGroup checkGroup = checkgroupService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,checkGroup);
    }
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id) {
        log.info("[检查项-根据ID查询]id:{}", id);
        List<Integer>list = checkgroup_checkitemDao.findCheckItemIdsByCheckGroupId(id);
        List<CheckItem> checkItemList = checkItemDao.findCheckItemIdsByCheckGroupId(list);
        return new Result(true,MessageConst.ACTION_SUCCESS,list);
    }
    @Transactional
    @PostMapping("/edit")
    public Result Edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        System.out.println(checkGroup);
        checkgroupService.update(checkGroup);
        checkgroup_checkitemDao.delete(checkGroup.getId());
        checkgroup_checkitemDao.Add(checkGroup.getId(), checkitemIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    @PostMapping("/deleteCheckGroupitemById")
    public Result DeleteGroupById(Integer id){
        checkgroupService.DeleteGroupById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }
    @GetMapping("/findAll")
    public Result FindAll(){
        List<CheckGroup> list = checkgroupService.findAll();
        return new Result(true,MessageConst.ACTION_SUCCESS,list);
    }
}
